package cn.zn.modules.appoin.service.impl;

import cn.zn.framework.common.utils.PageUtils;
import cn.zn.modules.appoin.dao.AppoinTimeRuleDao;
import cn.zn.modules.appoin.entity.*;
import cn.zn.modules.appoin.inter.AppoinTimeRuleInter;
import cn.zn.modules.appoin.service.*;
import cn.zn.modules.zzsys.inter.SysRoleInter;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.io.Serializable;
import java.util.Collection;
import java.util.List;


@Service("appoinTimeRuleService")
public class AppoinTimeRuleServiceImpl extends ServiceImpl<AppoinTimeRuleDao, AppoinTimeRuleEntity> implements AppoinTimeRuleService, AppoinTimeRuleInter {

    @Autowired
    AtrWeekService atrWeekService;
    @Autowired
    AtrWeekWorkTimeService atrWeekWorkTimeService;
    @Autowired
    AtrSpecialService atrSpecialService;
    @Autowired
    AtrSpecialWorkTimeService atrSpecialWorkTimeService;
    @Autowired
    private SysRoleInter sysRoleInter;

    @Override
    public PageUtils queryPage(AppoinTimeRuleEntity appoinTimeRule, IPage<AppoinTimeRuleEntity> page) {
        IPage<AppoinTimeRuleEntity> pageResult = this.page(page, new QueryWrapper<AppoinTimeRuleEntity>()
                .orderByDesc("CREATE_TIME_"));
        pageResult.getRecords().stream().forEach(o -> {
            o.setWorkerRoleName(sysRoleInter.queryRoleName(o.getWorkerRole()));
        });
        return new PageUtils(pageResult);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void changeState(String id) {
        // 更新启用状态的规则为未启用
        this.update(new UpdateWrapper<AppoinTimeRuleEntity>()
                .set("IS_ACTIVE_", false)
                .eq("IS_ACTIVE_", true));
        // 更新指定规则为启用
        this.update(new UpdateWrapper<AppoinTimeRuleEntity>()
                .set("IS_ACTIVE_", true)
                .eq("ID_", id));
    }

    @Override
    public AppoinTimeRuleEntity queryUse() {
        return super.getOne(new QueryWrapper<AppoinTimeRuleEntity>()
                .eq("is_active_", true));
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean saveRule(AppoinTimeRuleEntity entity) {
        if (entity.getIsActive() == null) {
            entity.setIsActive(false);
        }
        super.save(entity);
        // 保存周设置
        atrWeekService.saveWeek(entity.getId());
        return true;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean removeByIds(Collection<? extends Serializable> idList) {
        // 删除周设置
        List<Object> weekIds = atrWeekService.listObjs(new QueryWrapper<AtrWeekEntity>().in("RULE_ID_", idList).select("ID_"));
        if (!weekIds.isEmpty()) {
            atrWeekWorkTimeService.remove(new QueryWrapper<AtrWeekWorkTimeEntity>().in("WEEK_ID_", weekIds));
            atrWeekService.remove(new QueryWrapper<AtrWeekEntity>().in("RULE_ID_", idList));
        }
        // 删除例外
        List<Object> specialIds = atrSpecialService.listObjs(new QueryWrapper<AtrSpecialEntity>().in("RULE_ID_", idList).select("ID_"));
        if (!specialIds.isEmpty()) {
            atrSpecialWorkTimeService.remove(new QueryWrapper<AtrSpecialWorkTime>().in("SPECIAL_ID_", specialIds));
            atrSpecialService.remove(new QueryWrapper<AtrSpecialEntity>().in("RULE_ID_", idList));
        }
        // 删除规则
        super.removeByIds(idList);
        return true;
    }

    @Override
    public void saveDefaultRule(Long tenantId) {
        this.saveRule(AppoinTimeRuleEntity.builder()
                .activeDay(Byte.valueOf("14"))
                .interval(15)
                .isAuto(false)
                .tenantId(tenantId)
                .title("默认规则")
                .isActive(true)
                .build());
    }
}